package org.dizena.center.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.dizena.center.bean.User;
import org.dizena.common.base.enums.EnumHeaderKey;
import org.dizena.common.utils.ReqUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
@Tag(name = "User接口")
public class UserController {

    @Operation(summary = "Get")
    @GetMapping({"/get/{id}"})
    public User get(@PathVariable("id") String id) {
        String trace = ReqUtil.getHeader(EnumHeaderKey.TRACE_ID.getKey());
        log.info("post trace={}", trace);
        User user = new User();
        user.setId(id);
        user.setName("Get");
        user.setAge(18);
        return user;
    }

    @Operation(summary = "Post")
    @PostMapping({"/post"})
    public User post(@RequestBody User user) {
        String trace = ReqUtil.getHeader(EnumHeaderKey.TRACE_ID.getKey());
        log.info("post trace={}", trace);
        return user;
    }

    @Operation(summary = "Test")
    @GetMapping({"/test"})
    public Map<String,String> test() {
       Map<String,String> map = ReqUtil.getHeaderMap();
       log.info("map {}",map);
       return map;
    }

    @Operation(summary = "upload")
    @PostMapping({"/upload"})
    public void upload(@RequestParam("name") String name,
                       @RequestParam("file") MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            byte[] bs = file.getBytes();
            log.info("name:{},file:{},{}",name,fileName,bs.length);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Operation(summary = "request")
    @GetMapping({"/request"})
    public void request(HttpServletRequest request) {
        for (Cookie cookie : request.getCookies()) {
            log.info(cookie.getName()+":"+cookie.getValue());
        }
    }



}
